
//2982.找出出现至少三次的最长特殊子字符串II
class Solution {
public:
    int maximumLength(string s) {
        unordered_map<char ,vector<int>> m;
        int i=0,n=s.size();
        while(i<n)
        {
            int start=i;
            while(i<n&&s[i]==s[start]) i++;

            m[s[start]].push_back(i-start);
        }

        int ret=0;
        for(auto& [ch,nums]:m )
        {
            sort(nums.begin(),nums.end(),greater());
            nums.push_back(0);   //向后面补上两个数据是的数组长度永远大于等于3
            nums.push_back(0);

            ret=max({nums[2],min(nums[1],nums[0]-1),nums[0]-2,ret});
        }
        return ret==0?-1:ret;
    }
};